package com.sxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxy.controller.common.CustomException;
import com.sxy.dao.CategoryDao;
import com.sxy.pojo.Category;
import com.sxy.pojo.Dish;
import com.sxy.pojo.Setmeal;
import com.sxy.service.CategoryService;
import com.sxy.service.DishService;
import com.sxy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前判断是否有关联
     * @param id
     */
    @Override
    public void removeById(Long id) {
        //构造菜品查询条件
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id
        dqw.eq(Dish::getCategoryId,id);
        //查询当前分类是否关联菜品，如果关联,抛出一个业务异常
        int count1 = dishService.count(dqw);
        //判断count1
        if (count1 > 0 ){
            //已经关联菜品，抛出业务异常
            throw new CustomException("当前分类已经关联了菜品，无法删除！");
        }
        //创建套餐条件查询
        LambdaQueryWrapper<Setmeal> slq = new LambdaQueryWrapper<>();
        //添加查询条件
        slq.eq(Setmeal::getCategoryId,id);
        //查询当前分类是否关联套餐，如果关联,抛出一个业务异常
        int count2 = setmealService.count(slq);
        //判断count2
        if (count2 > 0){
            //已经关联套餐，抛出异常
            throw new CustomException("当前分类已经关联了套餐，无法删除！");
        }

        //正常删除
        super.removeById(id);
    }
}
